<template>
  <div>
    <el-row :gutter="15" class="">
      <el-form ref="elForm" :model="dataForm" :rules="dataRule" size="small" label-width="100px"
        label-position="right" :disabled="setting.readonly">
        <template v-if="!loading && formOperates">
          <el-col :span="12">
            <jnpf-form-tip-item label="文件名称">
              {{dataForm.fullname}}
            </jnpf-form-tip-item>
          </el-col>
          <el-col :span="12">
            <jnpf-form-tip-item label="版本">
              {{dataForm.version}}
            </jnpf-form-tip-item>
          </el-col>
          <el-col :span="12">
            <jnpf-form-tip-item label="创建人">
              {{dataForm.creatorUserId}}
            </jnpf-form-tip-item>
          </el-col>
          <!-- <el-col :span="12">
            <jnpf-form-tip-item label="创建时间">

              <template :formatter="jnpf.tableDateFormat">{{dataForm.creatorTime}}</template>
            </jnpf-form-tip-item>
          </el-col> -->
          <el-col :span="24">
            <jnpf-form-tip-item label="文件下载">
              <el-button size="mini" type="text" @click="handleDownLoad()">{{dataForm.filepath}}
              </el-button>
            </jnpf-form-tip-item>
          </el-col>
        </template>
        <SelectDialog v-if="selectDialogVisible" :config="currTableConf" :formData="dataForm"
          ref="selectDialog" @select="addForSelect" @close="selectDialogVisible=false" />
      </el-form>
    </el-row>
    <UserBox v-if="userBoxVisible" ref="userBox" @submit="submit" />
  </div>
</template>
<script>
import { getFormById } from '@/api/workFlow/FormDesign'
import request from '@/utils/request'
import { mapGetters } from "vuex";
import comMixin from '@/views/workFlow/workFlowForm/mixin';
import { getDataInterfaceRes } from '@/api/systemData/dataInterface'
import { getDictionaryDataSelector } from '@/api/systemData/dictionary'
import { getDefaultCurrentValueUserId } from '@/api/permission/user'
import { getDefaultCurrentValueDepartmentId } from '@/api/permission/organize'
import { getDateDay, getLaterData, getBeforeData, getBeforeTime, getLaterTime } from '@/components/Generator/utils/index.js'
import { thousandsFormat } from "@/components/Generator/utils/index"
export default {
  mixins: [comMixin],
  components: {},
  props: [],
  data() {
    return {
      visible: false,
      loading: false,
      formRef: 'elForm',
      setting: {},
      eventType: '',
      userBoxVisible: false,
      selectDialogVisible: false,
      currTableConf: {},
      addTableConf: {
      },
      //可选范围默认值
      ableAll: {
      },
      tableRows: {
      },
      currVmodel: "",
      dataForm: {
        id: '',
        flowId: '',
        status: 1,
        freeapproveruserid: '',
        flowUrgent: 1,
        fullname: '',
        fileextension: '',
        filepath: '',
        version: '',
        creatorUserId: '',
        creatorTime: '',
        vid: '',
        fullName: '',
        type: '',
        parentId: ''
      },
      tableRequiredData: {},
      dataRule:
      {



      },

      childIndex: -1,
      isEdit: false,
      interfaceRes: {
        fullname: [],
        fileextension: [],
        filepath: [],
      },
    }
  },
  computed: {
    formOperates() {
      return this.setting.formOperates
    },
    ...mapGetters(['userInfo'])
  },
  watch: {},
  created() {
    this.getFormById();
    if (this.dataForm.id == null || this.dataForm.id == '' && this.dataForm.id == undefined || this.dataForm.id == 0) {
      this.initDefaultData()

    }

  },
  mounted() { },
  methods: {
    // 下载文件
    handleDownLoad() {
      window.open(
        '/dev/api/extend/Document/Download/' + this.dataForm.vid,
        '_self'
      );
    },
    changeData(model, index) {
      this.isEdit = false
      this.childIndex = index
      let modelAll = model.split("-");
      let faceMode = "";
      for (let i = 0; i < modelAll.length; i++) {
        faceMode += modelAll[i];
      }
      for (let key in this.interfaceRes) {
        if (key != faceMode) {
          let faceReList = this.interfaceRes[key]
          for (let i = 0; i < faceReList.length; i++) {
            if (faceReList[i].relationField == model) {
              let options = 'get' + key + 'Options';
              if (this[options]) {
                this[options]()
              }
              this.changeData(key, index)
            }
          }
        }
      }
    },
    changeDataFormData(type, data, model, index, defaultValue) {
      if (!this.isEdit) {
        if (type == 2) {
          for (let i = 0; i < this.dataForm[data].length; i++) {
            if (index == -1) {
              this.dataForm[data][i][model] = defaultValue
            } else if (index == i) {
              this.dataForm[data][i][model] = defaultValue
            }
          }
        } else {
          this.dataForm[data] = defaultValue
        }
      }
    },
    //初始化默认数据
    initDefaultData() {
    },
    dataAll() {
    },
    getFormById() {
      getFormById("515258894776310277").then(res => {
        this.dataForm.flowId = res.data && res.data.flowId
        // this.encode = res.data&&res.data.encode
      })
    },
    selfGetInfo(dataForm) {
      //
      let docid = this.dataForm.id
      console.log(dataForm)
      request({
        url: `/api/extend/Document/getspinfo/` + docid,
        method: "GET"
      }).then(res => {
        let dat = res.data
        console.log(res.data)
        this.dataForm.id = dat.id
        this.dataForm.fullname = dat.fullName
        this.dataForm.version = dat.version
        this.dataForm.filepath = dat.filePath
        this.dataForm.creatorUserId = dat.creatorUserId
        this.dataForm.creatorTime = dat.creatorTime
        this.dataForm.vid = dat.vid
        this.dataForm.fullName = dat.filePath
        this.dataForm.type = dat.type
        this.dataForm.parentId = dat.parentId
      });

    },
    beforeSubmit() {
      const _data = this.dataList()
      return _data
    },
    selfInit() {
      this.dataAll()
    },
    exist() {
      let isOk = true
      for (let key in this.tableRequiredData) {
        if (this.dataForm[key] && Array.isArray(this.dataForm[key])) {
          for (let i = 0; i < this.dataForm[key].length; i++) {
            let item = this.dataForm[key][i]
            inner: for (let id in item) {
              let arr = this.tableRequiredData[key].filter(o => o.id === id) || []
              if (!arr.length) continue inner
              if (arr[0].required) {
                let msg = `${arr[0].name}不能为空`
                let boo = true
                if (arr[0].dataType === 'array') {
                  boo = !this.jnpf.isEmptyArray(item[id])
                } else {
                  boo = !this.jnpf.isEmpty(item[id])
                }
                if (!boo) {
                  this.$message({
                    message: msg,
                    type: 'error',
                    duration: 1000
                  })
                  isOk = false
                  break
                }
              }
            }
          }
        }
      }
      return isOk
    },


    openSelectDialog(key) {
      this.currTableConf = this.addTableConf[key]
      this.currVmodel = key
      this.selectDialogVisible = true
      this.$nextTick(() => {
        this.$refs.selectDialog.init()
      })
    },
    addForSelect(data) {
      for (let i = 0; i < data.length; i++) {
        let item = { ...this.tableRows[this.currVmodel], ...data[i] }
        this.dataForm[this.currVmodel].push(item)
      }
    },
    dateTime(timeRule, timeType, timeTarget, timeValueData, dataValue) {
      let timeDataValue = null;
      let timeValue = Number(timeValueData)
      if (timeRule) {
        if (timeType == 1) {
          timeDataValue = timeValue
        } else if (timeType == 2) {
          timeDataValue = dataValue
        } else if (timeType == 3) {
          timeDataValue = new Date().getTime()
        } else if (timeType == 4) {
          let previousDate = '';
          if (timeTarget == 1 || timeTarget == 2) {
            previousDate = getDateDay(timeTarget, timeType, timeValue)
            timeDataValue = new Date(previousDate).getTime()
          } else if (timeTarget == 3) {
            previousDate = getBeforeData(timeValue)
            timeDataValue = new Date(previousDate).getTime()
          } else {
            timeDataValue = getBeforeTime(timeTarget, timeValue).getTime()
          }
        } else if (timeType == 5) {
          let previousDate = '';
          if (timeTarget == 1 || timeTarget == 2) {
            previousDate = getDateDay(timeTarget, timeType, timeValue)
            timeDataValue = new Date(previousDate).getTime()
          } else if (timeTarget == 3) {
            previousDate = getLaterData(timeValue)
            timeDataValue = new Date(previousDate).getTime()
          } else {
            timeDataValue = getLaterTime(timeTarget, timeValue).getTime()
          }
        }
      }
      return timeDataValue;
    },
    time(timeRule, timeType, timeTarget, timeValue, formatType, dataValue) {
      let format = formatType == 'HH:mm' ? 'HH:mm:00' : formatType
      let timeDataValue = null
      if (timeRule) {
        if (timeType == 1) {
          timeDataValue = timeValue || '00:00:00'
          if (timeDataValue.split(':').length == 3) {
            timeDataValue = timeDataValue
          } else {
            timeDataValue = timeDataValue + ':00'
          }
        } else if (timeType == 2) {
          timeDataValue = dataValue
        } else if (timeType == 3) {
          timeDataValue = this.jnpf.toDate(new Date(), format)
        } else if (timeType == 4) {
          let previousDate = '';
          previousDate = getBeforeTime(timeTarget, timeValue)
          timeDataValue = this.jnpf.toDate(previousDate, format)
        } else if (timeType == 5) {
          let previousDate = '';
          previousDate = getLaterTime(timeTarget, timeValue)
          timeDataValue = this.jnpf.toDate(previousDate, format)
        }
      }
      return timeDataValue;
    },
    dataList() {
      var _data = JSON.parse(JSON.stringify(this.dataForm));



      return _data;
    },
    dataInfo(dataAll) {
      this.isEdit = true
      this.dataAll()
      this.childIndex = -1
    },
  },
}

</script>
